<?php
namespace App\Task\Gift;

use App\Helpers\AliyunUtil;
use App\Model\Gift;
use App\Model\Message;
use App\Model\NickNamePool;
use App\Model\UserProfile;
use Hyperf\Crontab\Annotation\Crontab;
/**
 * 定时发送漂流瓶
 * @Crontab(name="DriftTask", rule="*\/1 * * * *", callback="execute", memo="定时发送漂流瓶",singleton=false)
 */
class DriftTask
{
    public function execute()
    {
        return false;
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $userId = 5;
        $chatUserId = 169704;
        $giftIdArray = [136,126,123,122,116,105,99,98,96];
        $rand_keys = rand(0, 8);
        $giftId = empty($giftIdArray[$rand_keys]) ? 136 : $giftIdArray[$rand_keys] ;
        $giftItem = Gift::getInfo($giftId);
        //透传
        $onlineUserIds = UserProfile::getOnlineUsers(0, 300);
        $onlineUserIds = array_unique(array_merge($onlineUserIds, [$userId, $chatUserId]));
        //获取随机头像
        $avatar = UserProfile::pickAvatar(1);
        $nickname = NickNamePool::pickNickName(1);
        $ret = Message::sendSlient($onlineUserIds, 'SEND_GIFT_TIP', [
            'sender_name' => $nickname,
            'sender_id' => $userId,
            'avatar' => UserProfile::buildAvatar($avatar, 200),
            'icon' => AliyunUtil::imageResize(AliyunUtil::IMG_HOST_V2 . $giftItem['icon'], 120, 120),
            'gift_name' => $giftItem['name'],
            'gift_id' => $giftItem['id'],
            'gift_num' => 1
        ]);
        $logs = [
            'avatar' => $avatar,
            'sender_name' => $nickname,
            'sender_id' => $userId
        ];
        if(empty($ret)){
            log_info(__CLASS__ . "==end==" . date('Y-m-d H:i:s')."失败：".json_encode($logs)."礼物：".json_encode($giftItem).'礼物ID'.$giftId.'执行结果:'.json_encode($ret));
        }else{
            log_info(__CLASS__ . "==end==" . date('Y-m-d H:i:s')."用户信息：".json_encode($logs)."礼物：".json_encode($giftItem).'礼物ID'.$giftId.'执行结果：'.json_encode($ret));
        }

    }
}